package com.lixin.smartclassroom.ui.home.tab2.fragment

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.drake.channel.receiveEvent
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lixin.smartclassroom.databinding.FragmentTab2Binding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.PagerAdapter
import com.lixin.smartclassroom.ui.home.tab1.activity.MessageAct
import com.lixin.smartclassroom.ui.home.tab1.activity.SearchAct
import com.lixin.smartclassroom.ui.home.tab2.activity.AddCourseAct
import com.lixin.smartclassroom.ui.login.BindStudentAct
import com.lixinkeji.base.BaseFragment
import com.lixinkeji.extension.addIntArgument
import com.lixinkeji.extension.commonDialog
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description
 * Author: lwc
 * Date:2023/12/5 15:23
 */
class Tab2Fragment : BaseFragment<FragmentTab2Binding>() {
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = FragmentTab2Binding.inflate(inflater)


    override fun iniView() {
        initSlidingLayout()
    }

    private fun initSlidingLayout() {
        val titles = ArrayList<String>()
        val fragments = ArrayList<Fragment>()
        when (requireContext().user_role) {
            1 -> {
                titles.add("我孩子参与的")
                fragments.add(Tab2ChildFragment().addIntArgument("type", 2))
            }

            2 -> {
                titles.add("我参与的")
                fragments.add(Tab2ChildFragment().addIntArgument("type", 2))
            }

            3 -> {
                titles.add("我创建的")
                titles.add("我参与的")
                fragments.add(Tab2ChildFragment().addIntArgument("type", 1))
                fragments.add(Tab2ChildFragment().addIntArgument("type", 2))
                binding.btnAdd.visible()
            }
        }


        val mPageAdapter = PagerAdapter(childFragmentManager, fragments, titles)

        binding.apply {
            viewPager.adapter = mPageAdapter
            tabLayout.setViewPager(viewPager)
            tabLayout.onPageSelected(0)

        }

        receiveEvent<String>("showMessageCount") {
            binding.tvMessageCount.visible()
            binding.tvMessageCount.text = it
        }
    }

    override fun onClick() {
        binding.apply {
            btnAdd.onClick {
                start(AddCourseAct::class.java){
                    putExtra("type","1")
                }
            }
            imgSearch.onClick {
                start(SearchAct::class.java)
            }

            rlMessage.onClick {
                start(MessageAct::class.java)
            }
        }



    }
}